package com.gradecak.alfresco.mvc.webscript;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.alfresco.service.ServiceRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
  private static Logger LOGGER = LoggerFactory.getLogger(AuthenticationInterceptor.class);

  @Autowired
  private ServiceRegistry serviceRegistry;

  /**
   * In this case intercept the request BEFORE it reaches the controller.
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    LOGGER.info("Intercepting: " + request.getRequestURI());

    String ticket = request.getParameter("alf_ticket");

    if (StringUtils.hasText(ticket)) {
      serviceRegistry.getAuthenticationService().validate(ticket);
    }
    return true;
  }

}
